<template>
  <div class="searchj-box">
    <div class="row">
      <div class="left">
        <el-icon>
          <Search />
        </el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button type="primary" @click="getBrandList">查询结果</el-button>
      </div>
    </div>
    <div class="row">
      <span>输入搜索:</span>
      <el-input v-model="form.keyword" placeholder="品牌名称/关键字"></el-input>
    </div>
  </div>
  <div class="add-area">
    <div class="text">
      <el-icon><Tickets /></el-icon><span>数据列表</span>
    </div>
    <el-button @click="add(null)">添加</el-button>
  </div>
  <el-table :data="list" border style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55" align="center" />
    <el-table-column prop="id" label="编号" align="center" />
    <el-table-column prop="name" label="品牌名称" align="center" />
    <el-table-column prop="firstLetter" label="品牌首字母" align="center">
    </el-table-column>
    <el-table-column prop="sort" label="排序" align="center" />
    <el-table-column
      prop="factoryStatus"
      label="品牌制造商"
      width="120"
      align="center"
    >
      <template #default="{ row }">
        <el-switch
          v-model="row.factoryStatus"
          :active-value="1"
          :inactive-value="0"
          @change="switchFactoryStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column prop="showStatus" label="是否显示" align="center">
      <template #default="{ row }">
        <el-switch
          v-model="row.showStatus"
          :active-value="1"
          :inactive-value="0"
          @change="switchShowStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column prop="status" label="相关" width="180" align="center">
      <span>商品:</span>
      <el-button link type="primary">100</el-button>
      <span>评价:</span>
      <el-button link type="primary">1000</el-button>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="200" align="center">
      <template #default="{ row }">
        <el-button @click="add(row.id)">编辑</el-button>
      <el-button type="danger" @click="del(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
 <div class="foot">
  <div class="left">
      <el-select class="m-2" v-model="optionList" clearable placeholder="批量操作">
        <el-option v-for="item in options" ::key="item.value" :value="item.value" :label="item.label" />
      </el-select>
      <el-button type="primary" @click="ConfirmTn">确定</el-button>
    </div>
  <ElConfigProvider :locale="zhCn">
  <div class="page">
    <el-pagination
      v-model:current-page="form.pageNum"
      v-model:page-size="form.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="getBrandList"
      @current-change="getBrandList"
    />
  </div>
</ElConfigProvider>
 </div>
</template>
<script setup lang="ts">
import { ElConfigProvider } from "element-plus";
import { Search, Tickets } from "@element-plus/icons-vue";
import { getBrandListAPI,DeleteBrandApi,factoryStatusApi,showStatusApi ,getShowStatusApiApi} from "./api";
import { ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ref,reactive } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();


const add = (id: number | null) => {
  if (!id) {
    router.push("/pms/addBrand");
  } else {
    router.push(`/pms/updateBrand?id=${id}`);
  }
};

const form = reactive({
  pageNum: 1,
  pageSize: 10,
  keyword: "",
});
const list = ref<any[]>([]);
const total = ref(0)
const deleteList = ref<number[]>([])
const optionList = ref([])
const options = [
    { value: '0', label: '显示品牌', },
    { value: '1', label: '隐藏品牌', },
]
// 删除品牌
const del = (id: number) => {
  ElMessageBox.confirm('是否要删除该品牌?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    DeleteBrandApi(id).then((res) => {
      if (res.code === 200) {
        ElMessage.success('删除成功');
        getBrandList();
      }
    })
  }).catch(() => { })

}
// 修改品牌制造商状态
const switchShowStatus = (row: IBrandModel) => {
  showStatusApi(row.id!, row.showStatus)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功！");
      } else {
        row.showStatus = row.showStatus === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.showStatus = row.showStatus === 1 ? 0 : 1;
    });
};
// 修改品牌是否显示状态
const switchFactoryStatus = (row: IBrandModel) => {
  factoryStatusApi(row.id!, row.factoryStatus)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功！");
      } else {
        row.factoryStatus = row.factoryStatus === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.factoryStatus = row.factoryStatus === 1 ? 0 : 1;
    });
};


const handleSelectionChange = (val: IBrandModel[]) => {
    val.forEach(item => {
        list.value.find(sur => sur.id === item.id)
        deleteList.value.push(item.id)
    })
}
// 批量操作
const ConfirmTn = () => {
  if (deleteList.value.length == 0) {
        ElMessage.warning('请选择要操作的订单')
        return
    }
    if (optionList.value[0] == 0) {
      getShowStatusApiApi(deleteList.value,1).then(res => {
            if (res.code === 200) {
                ElMessage.success('修改成功')
                getBrandList();
            }
        })
    } else if (optionList.value[0] == 1) {
      getShowStatusApiApi(deleteList.value,0).then(res => {
            if (res.code === 200) {
                ElMessage.success('修改成功')
                getBrandList();
            }
        })
    }
}
// 封装品牌管理数据
const  getBrandList = () => {
  getBrandListAPI(form).then((res) => {
    if (res.code === 200) {
      list.value = res.data.list;
      total.value = res.data.total
      
    }
  });
};
getBrandList();
</script>
<style lang="less" scoped>
.searchj-box {
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;

  .row {
    padding: 20px 0;
    display: flex;

    &:first-child {
      justify-content: space-between;
    }

    &:last-child {
      width: 400px;

      span {
        width: 100px;
      }
    }
  }
}
:deep(.align-c),
:deep(.cell) {
  text-align: center !important;
}

.add-area {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  .text {
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
  }
}
.foot {
  display: flex;
  justify-content: space-between;
  padding-top: 30px;
}
</style>
